<template>
  <!--基本信息  -->
  <FormPanel
    ref="FormPanel"
    :list="initForm"
    :cols="2"
    :formType="formType"
    :labelWidth="160"
    :isSubmitBtn="false"
    @formChange="formChangeHandle"
    class="mtop"
  />
</template>

<script>
import { getAppId } from '@/assets/js/auth'; //获取登录用户

import { region } from '@/api/cms-common/region'; //行政区域
import { getRegion, getCommunity } from '@/api/cms-common/dealerdata/index'; //事业部，办事处

import { dictionary } from '@/utils/dictMixin';
import * as types from '@/api/cms-api/origin/originInfo/types';
import * as constructTypes from '@/api/cms-api/origin/construct/dealer/types';

import { dealerCommon, isArcfox } from '@/api/cms-api/origin/commonDealerType'; //经销商类型工具类

import vm from './bridge';

export default {
  mixins: [dictionary, region, dealerCommon],
  model: {
    prop: 'pdata',
    event: 'click'
  },
  props: {
    pdata: {
      type: Object,
      default: () => ({ auditStatus: -1 })
    },
    formType: {
      type: String,
      required: true
    },
    formOptions: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      initForm: this.initFormFun(),
      opneKey: 1
    };
  },
  mounted() {
    //初始化省份
    this.initProvince();
  },
  methods: {
    //初始化数据
    init() {
      let provinceId, cityId, deptId;
      //添加appid
      this.pdata.appIdUpper = getAppId().toLocaleUpperCase();
      if (-1 == this.pdata.districtId) {
        this.pdata.districtId = null;
      }
      console.log('werwefsddvsd', this.pdata);
      this.initForm.forEach(x => {
        //回显下拉选需要的ID
        if (x.fieldName === 'provinceId') {
          provinceId = this.pdata[x.fieldName];
        } else if (x.fieldName === 'cityId') {
          cityId = this.pdata[x.fieldName];
        } else if (x.fieldName === 'deptId') {
          deptId = this.pdata[x.fieldName];
        } else if (x.fieldName === 'fuelDealerStatus') {
          if ('beijing' == this.pdata.appId) {
            x.hidden = false;
          }
        } else if (x.fieldName === 'newenergyDealerStatus') {
          if ('beijing' == this.pdata.appId) {
            x.hidden = false;
          }
        } else if (x.fieldName === 'dealerType') {
          if (this.formType != 'show') {
            vm.$emit('changeDealerType', this.pdata[x.fieldName]);
            this.changeDealerTypeToDep(this.pdata[x.fieldName]);
          } else {
            this.changeDealerTypeToDep(this.pdata[x.fieldName]);
          }
        } else if (x.fieldName === 'ownBrand') {
          if (this.pdata[x.fieldName]) {
            let list = this.pdata[x.fieldName].split(',');
            this.pdata[x.fieldName] = list.map(x => parseInt(x));
          } else {
            this.pdata[x.fieldName] = [];
          }
        }
        //回显数据
        x.initialValue = this.pdata[x.fieldName];
      });
      this.initRegion(provinceId, cityId);
      this.changeDeptSelect(deptId);
    },
    formChangeHandle(val) {
      this.pdata = Object.assign(this.pdata, val);
      this.pdata.ownBrand = this.pdata.ownBrand.join(',');
      //传递数据到父组件里
      this.$emit('click', this.pdata);
      //触发外部的事件，以标志着本表单已经准备完毕
      this.$emit('formchange', this.pdata);
    },
    saveDialog() {
      this.$refs.FormPanel.SUBMIT_FORM();
    },
    //生成数据
    createDictListToDisabled(key, disabledList) {
      let dictList = this.createDictList(key);
      //如果不是，或者是空的array，则直接返回原始数据
      if (!Array.isArray(disabledList) || disabledList.length == 0) {
        return dictList;
      }
      dictList = Object.assign([], this.createDictList(key));
      dictList.forEach(x => {
        disabledList.forEach(y => {
          if (x.value === y) {
            x.disabled = true;
          }
        });
      });
      return dictList;
    },
    //生成数据(只能选择某某)
    createDictListToNotDisabled(key, disabledList) {
      let dictList = this.createDictList(key);
      //如果不是，或者是空的array，则直接返回原始数据
      if (!Array.isArray(disabledList) || disabledList.length == 0) {
        return dictList;
      }
      dictList = Object.assign([], this.createDictList(key));
      //关闭其他不需要的
      dictList.forEach(x => {
        disabledList.forEach(y => {
          if (x.value != y) {
            x.disabled = true;
          }
        });
      });
      return dictList;
    },
    //根据大客户类型变更事业部
    changeDealerTypeToDep(value, isChear = true) {
      //变更其事业部(包不好含大客户专属的机构)
      if (isChear) {
        this.initForm.find(item => item.fieldName === 'deptId').initialValue = '';
      }
      if (types.DEALER_INFO_DEALER_TYPE_BIG_CUSTOMER === value) {
        this.getBigcustomerData();
      } else {
        this.getNotBigcustomerData();
      }
    },
    //根据不同的状态展现不同的选项（如果直接在经销商信息维护里添加只能选择大客户）
    createDealerTypeByType() {
      if (this.formOptions.formType === types.DEALER_INFO_FORM_STATUS_MAINTAIN_OEM) {
        //只能选择大客户
        return this.getOriginInfoDealerTypeToNotDisabled();
        // return this.createDictListToNotDisabled('2503',[types.DEALER_INFO_DEALER_TYPE_BIG_CUSTOMER]);
      } else {
        return this.getOriginInfoDealerType();
      }
    },
    initRegion(provinceId, cityId) {
      this.initCity(provinceId, false);
      this.initTown(cityId, false);
    },
    //初始化所在省份
    async initProvince() {
      let pl = await this.createRegionList();
      this.initForm.find(item => item.fieldName === 'provinceId').itemList = pl;
    },
    //初始化所在市数据
    async initCity(provinceId, flag = true) {
      if (!provinceId || provinceId == null || typeof provinceId == 'undefined') {
        return;
      }
      let city = this.initForm.find(item => item.fieldName === 'cityId');
      //清空下级数据
      if (flag) {
        city.initialValue = '';
        let district = this.initForm.find(item => item.fieldName === 'districtId');
        district.initialValue = '';
        district.itemList = [];
      }
      //取省数据
      let rd = this.getRegionData();
      this.cityData = rd[provinceId].children;
      let cl = this.createSubRegionList(this.cityData);
      city.itemList = cl;
    },
    //初始化所在县市数据
    async initTown(cityId, flag = true) {
      if (!cityId || cityId == null || typeof cityId == 'undefined') {
        return;
      }
      let district = this.initForm.find(item => item.fieldName === 'districtId');
      //清空下级数据
      if (flag) {
        district.initialValue = '';
      }
      let td = this.cityData[cityId].children;
      let tl = this.createSubRegionList(td);
      district.itemList = tl;
    },
    //获取事业部下拉列表数据(只有‘大客户事业部’)
    async getBigcustomerData() {
      const dictList = await this._$getBigcustomerData();
      //if (res.resultCode === 200) {
      //this.initForm.find(item => item.fieldName === 'deptId').itemList = res.data.map(x => ({ value: parseInt(x.id), text: x.orgName }));
      //}
      this.initForm.find(item => item.fieldName === 'deptId').itemList = dictList;
    },
    //获取事业部下拉列表数据(不包含‘大客户事业部’)
    async getNotBigcustomerData() {
      const dictList = await this._$getNotBigcustomerData();
      //if (res.resultCode === 200) {
      //this.initForm.find(item => item.fieldName === 'deptId').itemList = res.data.map(x => ({ value: parseInt(x.id), text: x.orgName }));
      //}
      this.initForm.find(item => item.fieldName === 'deptId').itemList = dictList;
    },
    //事业部下拉改变执行方法，级联查询该事业部下的办事处
    async changeDeptSelect(deptId) {
      let initForm$officeId = this.initForm.find(item => item.fieldName === 'officeId');
      if (!deptId) {
        initForm$officeId.initialValue = '';
        initForm$officeId.itemList = [];
      } else {
        const res = await getCommunity(deptId);
        if (res.resultCode === 200) {
          initForm$officeId.itemList = res.data.map(x => ({ value: parseInt(x.id), text: x.orgName }));
        }
      }
    },
    //搜索数据
    initFormFun() {
      if(isArcfox()){
          return [
        {
          type: 'INPUT',
          label: '品牌',
          fieldName: 'appIdUpper',
          disabled: true,
          initialValue: getAppId().toLocaleUpperCase(),
          placeholder: '',
          noChangeDisabled: true,
          noWipeData: true
        },
        // { type: 'INPUT', label: '品牌', fieldName: 'appId', disabled: true, initialValue: getAppId(),placeholder:''},
        {
          type: 'INPUT',
          rules: [
            {
              required: true,
              message: '必填',
              trigger: 'blur'
            }
          ],
          label: '经销商简称',
          fieldName: 'dealerShortname',
          placeholder: '',
          disabled: false,
          initialValue: '',
          maxlength: 90
        },
        {
          type: 'INPUT',
          label: '经销商代码',
          fieldName: 'dealerCode',
          disabled: true,
          initialValue: '',
          placeholder: ''
        },
        { type: 'INPUT', label: '老DMS代码', fieldName: 'dealerCodeOld', initialValue: '', placeholder: '' },
        {
          type: 'INPUT',
          rules: [
            {
              required: true,
              message: '必填',
              trigger: 'blur'
            }
          ],
          label: '公司全称',
          fieldName: 'dealerName',
          disabled: false,
          selfCols: 2,
          rows: 1,
          initialValue: '',
          placeholder: '',
          maxlength: 300
        },
        {
          type: 'SELECT',
          label: '销售模式',
          fieldName: 'saleType',
          placeholder: '请选择',
          itemList: this.createDictList('6501'),
        },
        {
          type: 'SELECT',
          label: '渠道类型',
          fieldName: 'dealerType',
          placeholder: '请选择',
          initialValue: '',
          itemList: this.createDealerTypeByType(),
        },
        {
          type: 'SELECT',
          label: '开票资质',
          fieldName: 'billType',
          placeholder: '请选择',
          initialValue:  '',
          itemList: this.createDictList('1004'),
        },
        {
          type: 'SELECT',
          label: '店面等级',
          fieldName: 'constructLevel',
          placeholder: '请选择',
          initialValue: '',
          itemList: this.createDictList('2554'),
        },

        { type: 'SELECT', label: '经销商级别', fieldName: 'shopLevel', disabled: false, placeholder: '', initialValue: '', itemList: this.createDictList('2540') },
        // { type: 'SELECT', label: '细分类别', fieldName: 'subType',disabled: true, placeholder: '', initialValue: '', itemList: this.createDictList('2539'),
        // 	hidden: !isArcfox(),
        // },
        { type: 'SELECT', label: '建设类型', fieldName: 'constructType', disabled: false, placeholder: '', initialValue: '', itemList: this.createDictList('2541') },

        {
          type: 'SELECT',
          label: '事业部',
          fieldName: 'deptId',
          placeholder: '',
          initialValue: '',
          rules: [
            {
              required: true,
              message: '必填',
              trigger: 'blur'
            }
          ],
          disabled: false,
          itemList: [],
          change: val => {
            this.changeDeptSelect(val);
          }
        },
        {
          type: 'SELECT',
          rules: [
            {
              required: true,
              message: '必填',
              trigger: 'blur'
            }
          ],
          label: '办事处',
          fieldName: 'officeId',
          disabled: false,
          placeholder: '',
          initialValue: '',
          itemList: []
        },
        {
          type: 'SELECT',
          rules: [
            {
              required: true,
              message: '必填',
              trigger: 'blur'
            }
          ],
          label: '启用标记',
          fieldName: 'status',
          disabled: false,
          placeholder: '',
          initialValue: '',
          itemList: this.createDictList('1003')
        },
        {
          type: 'SELECT',
          label: '省份',
          fieldName: 'provinceId',
          disabled: false,
          placeholder: '',
          initialValue: '',
          itemList: [],
          change: props => {
            this.initCity(props);
          }
        },
        { type: 'SELECT', label: '城市', fieldName: 'cityId', placeholder: '', disabled: false, initialValue: '', itemList: [], change: props => this.initTown(props) },
        { type: 'SELECT', label: '县区', fieldName: 'districtId', placeholder: '', disabled: false, initialValue: '', itemList: [] },
        {
          type: 'SELECT',
          rules: [
            {
              required: true,
              message: '必填',
              trigger: 'blur'
            }
          ],
          label: '经营状态',
          fieldName: 'dealerStatus',
          placeholder: '',
          disabled: false,
          initialValue: '',
          itemList: this.createDictList('2552')
        },
        {
          type: 'SELECT',
          label: '燃油车经营状态',
          fieldName: 'fuelDealerStatus',
          hidden: true,
          placeholder: '',
          disabled: false,
          initialValue: '',
          itemList: this.createDictList('8051')
        },
        {
          type: 'SELECT',
          label: '新能源经营状态',
          hidden: true,
          fieldName: 'newenergyDealerStatus',
          placeholder: '',
          disabled: false,
          initialValue: '',
          itemList: this.createDictList('8051')
        },
        { type: 'SELECT', label: '是否启用中央补贴', fieldName: 'isCentrelAllowance', placeholder: '', disabled: false, initialValue: '', itemList: this.createDictList('1004') },
        { type: 'SELECT', label: '是否启用地方补贴', fieldName: 'isRegionalAllowance', placeholder: '', disabled: false, initialValue: '', itemList: this.createDictList('1004') },
        { type: 'INPUT', label: '系统管理员', fieldName: 'systemManager', placeholder: '', disabled: false, initialValue: '', maxlength: 90 },
        { type: 'INPUT', label: '管理员电话', fieldName: 'sysManagerPhone', placeholder: '', disabled: false, initialValue: '', maxlength: 20 },
        { type: 'SELECT', label: '开票是否包含地方补贴', fieldName: 'isContainRAllowance', placeholder: '', disabled: false, initialValue: '', itemList: this.createDictList('1004') },
        { type: 'INPUT_NUMBER', label: '保证金金额', fieldName: 'cashDeposit', disabled: false, initialValue: undefined, precision: 2, min: 0, max: 999999999999999.99 },
        { type: 'MULTIPLE_SELECT', label: '体系内品牌', fieldName: 'ownBrand', placeholder: '', disabled: false, initialValue: [], itemList: this.createDictList('2545') },
        { type: 'INPUT_NUMBER', label: '设计费金额', fieldName: 'designFee', disabled: false, initialValue: undefined, precision: 2, min: 0, max: 999999999999999.99 },
        { type: 'SELECT', label: '建店标准', fieldName: 'constructStandard', placeholder: '', disabled: false, initialValue: '', itemList: this.createDictList('2553') },
        { type: 'INPUT_NUMBER', label: '补贴金额', fieldName: 'allowanceFee', disabled: false, initialValue: undefined, precision: 2, min: 0, max: 999999999999999.99 },
        { type: 'TEXT_AREA', label: '其他', rows: 2, selfCols: 2, maxlength: 1000, fieldName: 'otherDesc', placeholder: '', disabled: false, initialValue: '' },
        {
          type: 'UPLOAD_FILE',
          label: '设计费附件',
          fieldName: 'designFeeEnclosureList',
          initialValue: [],
          disabled: false,
          upload: {
            actionUrl: '/api/file/oss/upload', //上传的地址
            limit: 99, //上传数量
            fileTypes: ['jpg', 'jpeg', 'png', 'gif', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'txt', 'pdf', 'rar', 'zip']
          }
        },
        {
          type: 'UPLOAD_FILE',
          label: '保证金附件',
          fieldName: 'cashDepositEnclosureList',
          disabled: false,
          initialValue: [],
          upload: {
            actionUrl: '/api/file/oss/upload', //上传的地址
            limit: 99, //上传数量
            fileTypes: ['jpg', 'jpeg', 'png', 'gif', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'txt', 'pdf', 'rar', 'zip']
          }
        }
      ];
      }else{
        return [
        {
          type: 'INPUT',
          label: '品牌',
          fieldName: 'appIdUpper',
          disabled: true,
          initialValue: getAppId().toLocaleUpperCase(),
          placeholder: '',
          noChangeDisabled: true,
          noWipeData: true
        },
        // { type: 'INPUT', label: '品牌', fieldName: 'appId', disabled: true, initialValue: getAppId(),placeholder:''},
        {
          type: 'INPUT',
          rules: [
            {
              required: true,
              message: '必填',
              trigger: 'blur'
            }
          ],
          label: '经销商简称',
          fieldName: 'dealerShortname',
          placeholder: '',
          disabled: false,
          initialValue: '',
          maxlength: 90
        },
        {
          type: 'INPUT',
          label: '经销商代码',
          fieldName: 'dealerCode',
          disabled: true,
          initialValue: '',
          placeholder: ''
        },
        { type: 'INPUT', label: '老DMS代码', fieldName: 'dealerCodeOld', initialValue: '', placeholder: '' },
        {
          type: 'INPUT',
          rules: [
            {
              required: true,
              message: '必填',
              trigger: 'blur'
            }
          ],
          label: '公司全称',
          fieldName: 'dealerName',
          disabled: false,
          selfCols: 2,
          rows: 1,
          initialValue: '',
          placeholder: '',
          maxlength: 300
        },
        { type: 'SELECT', label: '经销商级别', fieldName: 'shopLevel', disabled: false, placeholder: '', initialValue: '', itemList: this.createDictList('2540') },
        { type: 'SELECT', label: '渠道类型', fieldName: 'dealerType', disabled: false, placeholder: '', initialValue: '', itemList: this.createDealerTypeByType() },
        // { type: 'SELECT', label: '细分类别', fieldName: 'subType',disabled: true, placeholder: '', initialValue: '', itemList: this.createDictList('2539'),
        // 	hidden: !isArcfox(),
        // },
        { type: 'SELECT', label: '建设类型', fieldName: 'constructType', disabled: false, placeholder: '', initialValue: '', itemList: this.createDictList('2541') },

        {
          type: 'SELECT',
          label: '事业部',
          fieldName: 'deptId',
          placeholder: '',
          initialValue: '',
          rules: [
            {
              required: true,
              message: '必填',
              trigger: 'blur'
            }
          ],
          disabled: false,
          itemList: [],
          change: val => {
            this.changeDeptSelect(val);
          }
        },
        {
          type: 'SELECT',
          rules: [
            {
              required: true,
              message: '必填',
              trigger: 'blur'
            }
          ],
          label: '办事处',
          fieldName: 'officeId',
          disabled: false,
          placeholder: '',
          initialValue: '',
          itemList: []
        },
        {
          type: 'SELECT',
          rules: [
            {
              required: true,
              message: '必填',
              trigger: 'blur'
            }
          ],
          label: '启用标记',
          fieldName: 'status',
          disabled: false,
          placeholder: '',
          initialValue: '',
          itemList: this.createDictList('1003')
        },
        {
          type: 'SELECT',
          label: '省份',
          fieldName: 'provinceId',
          disabled: false,
          placeholder: '',
          initialValue: '',
          itemList: [],
          change: props => {
            this.initCity(props);
          }
        },
        { type: 'SELECT', label: '城市', fieldName: 'cityId', placeholder: '', disabled: false, initialValue: '', itemList: [], change: props => this.initTown(props) },
        { type: 'SELECT', label: '县区', fieldName: 'districtId', placeholder: '', disabled: false, initialValue: '', itemList: [] },
        {
          type: 'SELECT',
          rules: [
            {
              required: true,
              message: '必填',
              trigger: 'blur'
            }
          ],
          label: '经营状态',
          fieldName: 'dealerStatus',
          placeholder: '',
          disabled: false,
          initialValue: '',
          itemList: this.createDictList('2552')
        },
        {
          type: 'SELECT',
          label: '燃油车经营状态',
          fieldName: 'fuelDealerStatus',
          hidden: true,
          placeholder: '',
          disabled: false,
          initialValue: '',
          itemList: this.createDictList('8051')
        },
        {
          type: 'SELECT',
          label: '新能源经营状态',
          hidden: true,
          fieldName: 'newenergyDealerStatus',
          placeholder: '',
          disabled: false,
          initialValue: '',
          itemList: this.createDictList('8051')
        },
        { type: 'SELECT', label: '是否启用中央补贴', fieldName: 'isCentrelAllowance', placeholder: '', disabled: false, initialValue: '', itemList: this.createDictList('1004') },
        { type: 'SELECT', label: '是否启用地方补贴', fieldName: 'isRegionalAllowance', placeholder: '', disabled: false, initialValue: '', itemList: this.createDictList('1004') },
        { type: 'INPUT', label: '系统管理员', fieldName: 'systemManager', placeholder: '', disabled: false, initialValue: '', maxlength: 90 },
        { type: 'INPUT', label: '管理员电话', fieldName: 'sysManagerPhone', placeholder: '', disabled: false, initialValue: '', maxlength: 20 },
        { type: 'SELECT', label: '开票是否包含地方补贴', fieldName: 'isContainRAllowance', placeholder: '', disabled: false, initialValue: '', itemList: this.createDictList('1004') },
        { type: 'INPUT_NUMBER', label: '保证金金额', fieldName: 'cashDeposit', disabled: false, initialValue: undefined, precision: 2, min: 0, max: 999999999999999.99 },
        { type: 'MULTIPLE_SELECT', label: '体系内品牌', fieldName: 'ownBrand', placeholder: '', disabled: false, initialValue: [], itemList: this.createDictList('2545') },
        { type: 'INPUT_NUMBER', label: '设计费金额', fieldName: 'designFee', disabled: false, initialValue: undefined, precision: 2, min: 0, max: 999999999999999.99 },
        { type: 'SELECT', label: '建店标准', fieldName: 'constructStandard', placeholder: '', disabled: false, initialValue: '', itemList: this.createDictList('2553') },
        { type: 'INPUT_NUMBER', label: '补贴金额', fieldName: 'allowanceFee', disabled: false, initialValue: undefined, precision: 2, min: 0, max: 999999999999999.99 },
        { type: 'SELECT', label: '店面等级', fieldName: 'constructLevel', placeholder: '', disabled: false, initialValue: '', itemList: this.createDictList('2554') },
        { type: 'TEXT_AREA', label: '其他', rows: 2, selfCols: 2, maxlength: 1000, fieldName: 'otherDesc', placeholder: '', disabled: false, initialValue: '' },
        {
          type: 'UPLOAD_FILE',
          label: '设计费附件',
          fieldName: 'designFeeEnclosureList',
          initialValue: [],
          disabled: false,
          upload: {
            actionUrl: '/api/file/oss/upload', //上传的地址
            limit: 99, //上传数量
            fileTypes: ['jpg', 'jpeg', 'png', 'gif', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'txt', 'pdf', 'rar', 'zip']
          }
        },
        {
          type: 'UPLOAD_FILE',
          label: '保证金附件',
          fieldName: 'cashDepositEnclosureList',
          disabled: false,
          initialValue: [],
          upload: {
            actionUrl: '/api/file/oss/upload', //上传的地址
            limit: 99, //上传数量
            fileTypes: ['jpg', 'jpeg', 'png', 'gif', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'txt', 'pdf', 'rar', 'zip']
          }
        }
      ];
      }
    }
  }
};
</script>
